// src/store/service/article.ts
import apiService from "@service/index";

const articleApiService = apiService
  .enhanceEndpoints({ addTagTypes: ["article"] })
  .injectEndpoints({
    endpoints: (builder) => ({
      // 获取文章详情
      requestArticle: builder.query<ArticleResponse, string>({
        query: (id) => ({ url: `/articles/${id}` }),
        providesTags: ["article"],
      }),
      collectArticle: builder.mutation({
        query: (id: string) => ({
          url: "/article/collections",
          method: "POST",
          body: { target: id },
        }),
        invalidatesTags: ["article"],
      }),
      // 取消收藏文章
      unCollectArticle: builder.mutation({
        query: (id: string) => ({
          url: `/article/collections/${id}`,
          method: "DELETE",
        }),
        invalidatesTags: ["article"],
      }),
      favouriteArticle: builder.mutation({
        query: (id: string) => ({
          url: "/article/likings",
          method: "POST",
          body: { target: id },
        }),
        invalidatesTags: ["article"],
      }),
      // 取消点赞文章
      unFavouriteArticle: builder.mutation({
        query: (id: string) => ({
          url: `/article/likings/${id}`,
          method: "DELETE",
        }),
        invalidatesTags: ["article"],
      }),
    }),
  });

export const { useRequestArticleQuery,useCollectArticleMutation, useUnCollectArticleMutation,useFavouriteArticleMutation, useUnFavouriteArticleMutation } = articleApiService;